import 'package:flutter/material.dart';
import 'package:yuan_hang_app/base/base_state.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/http/api.dart';
import 'package:yuan_hang_app/http/http_utils.dart';
import 'package:yuan_hang_app/views/home/view/leads/widget/leads_info_list.dart';
import 'package:yuan_hang_app/widget/w_toast.dart';

import '../../../../widget/common_attachment.dart';
import '../../model/leads.dart';

class LeadsPoolDetailPage extends StatefulWidget {
  final int clueId;
  const LeadsPoolDetailPage({super.key, required this.clueId});

  @override
  BaseState<LeadsPoolDetailPage> createState() => _LeadsPoolDetailPageState();
}

class _LeadsPoolDetailPageState extends BaseState<LeadsPoolDetailPage> {
  Leads leadsInfo = Leads();
  GlobalKey<CommonAttachmentState>? picKey;

  @override
  void initState() {
    super.initState();
    picKey = GlobalKey(debugLabel: "picKey");
    fetchDataInfo(widget.clueId);
  }

  @override
  FullBasePage buildState(BuildContext context) {
    return FullBasePage(
        title: "线索详情",
        child: LeadsInfoList(leads: leadsInfo,picKey: picKey!,)
    );
  }

  fetchDataInfo(int clueId) {
    HttpUtils().get("${Api.getClueDetail}/${clueId}").then((value) {
      if (value.resultCode == 200) {
        if(value.data != null) {
          leadsInfo = Leads.fromJson(value.data);
          List picUrlList = (leadsInfo.filesList ?? [])
              .map((item) => item["url"])
              .toList();
          picKey!.currentState?.refreshList(picUrlList);
          setState(() {});
        }
      } else {
        Toast.toast(value.errMsg);
      }
    });
  }
}
